package com.plian.system.controller.im;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.im.InvestmentPlanRecord;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.im.IInvestmentPlanRecordService;
import com.plian.system.vo.im.InvestmentPlanRecordVO;
import com.plian.system.wrapper.im.InvestmentPlanRecordWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.util.List;

/**
 * 控制器
 *
 * @author
 * @since 2019-09-27
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/investmentPlanRecord")
@Api(value = "InvestmentPlanRecordController", tags = "投资计划备案 控制器")
public class InvestmentPlanRecordController extends BaseController {

	private IInvestmentPlanRecordService investmentPlanRecordService;

	private InvestmentPlanRecordWrapper investmentPlanRecordWrapper;

	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入 id")
	public R<InvestmentPlanRecordVO> detail(@RequestBody InvestmentPlanRecord investmentPlanRecord) {
		return investmentPlanRecordService.getDetailObj(investmentPlanRecord);
	}

	@PostMapping("/list")
	@ApiOperation(value = "列表", notes = "传入 query")
	public ListResult<InvestmentPlanRecordVO> list(@RequestBody Query query) {
		return investmentPlanRecordService.list(query);
	}

	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("投资计划备案-提交")
	@ApiOperation(value = "保存", notes = "传入 investmentPlanRecord")
	public R<InvestmentPlanRecordVO> submit(@Valid @RequestBody InvestmentPlanRecord investmentPlanRecord) {
		boolean result = investmentPlanRecordService.saveOrUpdate(investmentPlanRecord);
		InvestmentPlanRecordVO planRecordVO = investmentPlanRecordWrapper.entityToVO(investmentPlanRecord);
		return result ? R.data(planRecordVO): new R(CommonCode.FAIL);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("投资计划备案-删除")
	@ApiOperation(value = "删除", notes = "传入 ids")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return investmentPlanRecordService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 下载doc
	 */
	@GetMapping("/buildword")
	@ApiOperation(value = "下载doc", notes = "传入 investmentPlanRecord")
	public void buildword(@RequestBody InvestmentPlanRecord investmentPlanRecord, HttpServletResponse response) {
		File buildword = investmentPlanRecordService.buildword(investmentPlanRecord, response);

	}

}
